/**
 * Copyright (c) Software Institute of PKU
 * All rights reserved. 
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 */
package edu.pku.sei.modeler.model.core;

import java.util.Iterator;

import org.eclipse.emf.ecore.EClass;
import org.w3c.dom.Element;

import edu.pku.sei.gefcommon.descriptor.annotation.ListProperty;
import edu.pku.sei.gefcommon.descriptor.annotation.MethodProperty;
import edu.pku.sei.modeler.model.common.MetaTypedList;
import edu.pku.sei.modeler.model.xml.XMLUtility;

/**
 * @author zl
 * @author xy
 *
 */
public class MetaEnum extends MetaDataType {
	MetaTypedList enumLiterals;
	
	MetaEnumLiteral getEnumLiteral(int value) {
		for(Iterator i = enumLiterals.iterator(); i.hasNext(); ) {
			MetaEnumLiteral literal = (MetaEnumLiteral) i.next();
			if( literal.getValue() == value ) {
				return literal;
			}
		}
		
		return null;		
	}
	
	MetaEnumLiteral getEnumLiteral(String name) {
		for(Iterator i = enumLiterals.iterator(); i.hasNext(); ) {
			MetaEnumLiteral literal = (MetaEnumLiteral) i.next();
			if( literal.getName().equals( name ) ) {
				return literal;
			}
		}
		
		return null;
	}
	
	@MethodProperty(name=CorePackage.ENUM_ENUMLITERALS,id="ENUMERATION_GET_ENUMLITERAL",
			catalog="Model"
			)
	@ListProperty(allowTypes = { MetaEnumLiteral.class }, typeString = CorePackage.__METAENUMLITERAL__, newInstanceMethod = "newInstance"
			)
	public MetaTypedList getEnumLiterals(){
		if(enumLiterals == null) {
			enumLiterals = new MetaTypedList(MetaEnumLiteral.class, this, CorePackage.ENUM_ENUMLITERALS);
		}
		return enumLiterals;
	}
	public MetaModelElement newInstance(String str){
		return super.newInstance(str);
	}
	public void serialize(Element root) {
		super.serialize(root);
		XMLUtility.xmlInstance.serializeNodes(root, CorePackage.ENUM_ENUMLITERALS, enumLiterals);
	}
	
	public void deSerialize(Element root) {
		super.deSerialize(root);
		this.getEnumLiterals().addAll(XMLUtility.xmlInstance.deSerializeNodes(root, CorePackage.ENUM_ENUMLITERALS));
	}
	
	@Override
	protected EClass eStaticClass() {
		return EMetaModelPackage.Literals.EMETA_ENUM;
	}
	
	@Override
	public Object eGet(int featureID, boolean resolve, boolean coreType) {
		switch (featureID) {
			case EMetaModelPackage.EMETA_ENUM__ENUM_LITERALS:
				return getEnumLiterals();
		}
		return super.eGet(featureID, resolve, coreType);
	}
}
